﻿using System;
using System.Collections.Generic;
using BSDS.Commands.Commands.Implementation;
using BSDS.Core.Data.Local;
using BSDS.Core.Model.Services;
using BSDS.Core.Model.Services.Jobs;

namespace BSDS.Commands.Commands.Creation.Implementation
{
    public class DownloadCommandCreator : CommandCreatorBase
    {
        private readonly DownloadService downloadService;
        private readonly JobService jobService;
        private readonly JobRepository jobRepository;
        private readonly DataRepository dataRepository;

        public DownloadCommandCreator(DownloadService downloadService, JobService jobService, 
            JobRepository jobRepository, DataRepository dataRepository)
        {
            this.downloadService = downloadService;
            this.jobService = jobService;
            this.jobRepository = jobRepository;
            this.dataRepository = dataRepository;
        }

        protected override bool Supports(Dictionary<string, string> parameters)
        {
            return parameters.Count >= 1 && parameters.ContainsKey("download");
        }

        protected override ICommand Create(Dictionary<string, string> parameters)
        {
            var configuration = CreateConfiguration(parameters);

            return Create(configuration);
        }

        protected ICommand Create(DownloadCommandConfiguration configuration)
        {
            return new DownloadCommand(downloadService, configuration, jobService, jobRepository, dataRepository);
        }

        protected DownloadCommandConfiguration CreateConfiguration(Dictionary<string, string> parameters)
        {
            var dataSourceName = parameters["download"];
            var format = DataFormat.Xml;
            var extension = format.Value;

            var portal = false;
            if (parameters.ContainsKey("portal"))
            {
                portal = true;
            }

            if (parameters.ContainsKey("format"))
            {
                format = DataFormat.Parse(parameters["format"]);
                extension = format.Value;

                // Override default pipe extension for portal compatibility
                if (portal && format == DataFormat.Pipe)
                {
                    extension = "txt";  
                }
            }

            var outFile = string.Format("{0}_{1}.{2}", dataSourceName, DateTime.Now.ToString("yyyy-MM-dd"), extension); // TODO: Make the format configurable
            if (parameters.ContainsKey("outfile"))
            {
                outFile = parameters["outfile"];
            }

            var resumeCompletedJob = false;
            var resumedJobId = string.Empty;
            if (parameters.ContainsKey("remsumeCompletedJob"))
            {
                resumeCompletedJob = true;
                resumedJobId = parameters["remsumeCompletedJob"];
            }

            var configuration = new DownloadCommandConfiguration(dataSourceName, format, outFile, portal, resumeCompletedJob, resumedJobId);
            return configuration;
        }
    }
}